package com.wms.controller;

import com.wms.config.Result;
import com.wms.service.ChartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("charts")
@CrossOrigin
public class ChartController {
    @Autowired
    ChartsService chartsService;

    // 药品库存统计
    @RequestMapping("chartsA")
    public Result chartsA() {
        List<HashMap> list = chartsService.chartA();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();

        for(HashMap item : list) {
            nameList.add((String) item.get("Dname"));
            valueList.add(Integer.valueOf(item.get("Dnumber").toString()));
        }

        Map<String, Object> map = new HashMap<>();
        map.put("name", nameList);
        map.put("number", valueList);
        return Result.success(map);
    }

    // 订单状态统计
    @RequestMapping("chartsB")
    public Result chartsB() {
        List<HashMap> list = chartsService.chartB();
        List<String> statusList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        for(HashMap item : list) {
            statusList.add((String) item.get("status"));
            countList.add(Integer.valueOf(item.get("count").toString()));
        }

        return Result.success(Map.of(
                "status", statusList,
                "count", countList
        ));
    }

    // 仓库容量统计
    @RequestMapping("chartsC")
    public Result chartsC() {
        List<HashMap> list = chartsService.chartC();
        List<String> nameList = new ArrayList<>();
        List<Integer> quantityList = new ArrayList<>();
        List<Integer> capacityList = new ArrayList<>();

        for(HashMap item : list) {
            nameList.add((String) item.get("Wname"));
            quantityList.add(Integer.valueOf(item.get("Wquantity").toString()));
            capacityList.add(Integer.valueOf(item.get("Wmaxcapacity").toString()));
        }

        return Result.success(Map.of(
                "name", nameList,
                "quantity", quantityList,
                "capacity", capacityList
        ));
    }

    // 用户角色统计
    @RequestMapping("chartsD")
    public Result chartsD() {
        List<HashMap> list = chartsService.chartD();
        List<String> roleList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        for(HashMap item : list) {
            int roleId = Integer.parseInt(item.get("Urole").toString());
            roleList.add(roleId == 1 ? "管理员" : "普通用户");
            countList.add(Integer.valueOf(item.get("count").toString()));
        }

        return Result.success(Map.of(
                "role", roleList,
                "count", countList
        ));
    }

    // 药品分类统计
    @RequestMapping("chartsE")
    public Result chartsE() {
        List<HashMap> list = chartsService.chartE();
        List<String> categoryList = new ArrayList<>();
        List<Integer> totalList = new ArrayList<>();

        for(HashMap item : list) {
            categoryList.add((String) item.get("Dcategory"));
            totalList.add(Integer.valueOf(item.get("total").toString()));
        }

        return Result.success(Map.of(
                "category", categoryList,
                "total", totalList
        ));
    }
}